#include "Skydome.h"

Skydome::Skydome()
:Entity()
{

}

Skydome::~Skydome()
{

}

FWint Skydome::Init()
{
	FWint err = 0;

	_Mesh = new Mesh("Resources\\Meshes\\", "skydome.snk");
	err += _Mesh->Init();

	_Texture = new Texture(6, "Resources\\Textures\\",
		"Skydome_0.tga;Skydome_1.tga;Skydome_2.tga;Skydome_3.tga;Skydome_4.tga;Skydome_5.tga");
	_Texture->SetTargetType(FW_TEXTURE_CUBE_MAP);
	err += _Texture->Init();

	_Shader = new Shader("Resources\\Shaders\\", "skydome.vsh", "skydome.fsh");
	err += _Shader->Init();

	return err;
}

void Skydome::Update(FWfloat deltaTime)
{
	SetRotateY(0.001f);
	Entity::Update(deltaTime);
}

void Skydome::Draw()
{
	glUseProgram(_Shader->ProgramObject);

	if (_Shader->CubeSampler != -1)
	{
		glBindTexture(GL_TEXTURE_CUBE_MAP, _Texture->TBOs[0]);
		glUniform1i(_Shader->CubeSampler, 0);
	}

	glBindBuffer(GL_ARRAY_BUFFER, _Mesh->VBO);

	if (_Shader->Position != -1)
	{
		glEnableVertexAttribArray(_Shader->Position);
		glVertexAttribPointer(_Shader->Position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
	}
	if (_Shader->Coordinate != -1)
	{
		glEnableVertexAttribArray(_Shader->Coordinate);
		glVertexAttribPointer(_Shader->Coordinate, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char*)0 + sizeof(Vector3));
	}
	if (_Shader->MatrixWVP != -1)
	{
		Matrix WVP = m_LocalWorld * CameraManager::_CurrentCamera->View * CameraManager::_CurrentCamera->Projection;
		glUniformMatrix4fv(_Shader->MatrixWVP, 1, GL_FALSE, (FWfloat*)&WVP);
	}

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _Mesh->IBO);
	glDrawElements(GL_TRIANGLES, _Mesh->IBOCount, GL_UNSIGNED_SHORT, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}

void Skydome::Destroy()
{
	_Mesh->Destroy();
	_Texture->Destroy();
	_Shader->Destroy();
}